﻿using System;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;


namespace cMES4001
{
    [Plug(ID = "cMES4001",
        Name = "产能报表查询",
        Description = "产能信息查询",
        Parent = "0,报表查询")]
    public partial class cMES4001 : StdView
    {
        public cMES4001()
        {
            InitializeComponent();
        }

        private void InitialLineNameAndGroup()
        {
            ExecutionResult<dynamic> result;
            result = new ExecutionResult<dynamic>();
            //query group name list
            result = Services.ServiceMethod.GetGroupNameList();
            if (result.Status)
            {
                this.combGroup.Items.Clear();
                foreach (var group in result.Data)
                {
                    this.combGroup.Items.Add(group.group_name);
                }
            }
            //query line name list
            result = Services.ServiceMethod.GetLineNameList();
            if (result.Status)
            {
                this.combLineName.Items.Clear();
                foreach (var line in result.Data)
                {
                    this.combLineName.Items.Add(line);
                }
            }
        }

        private void ClearFormInput()
        {
            this.txtMO.Text = "";
            this.txtModel.Text = "";
            this.dtpWorkDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            this.InitialLineNameAndGroup();
        }
        private bool CheckInputCondition()
        {
            bool bResult = true;

            if (string.IsNullOrEmpty(this.dtpWorkDate.Text.Trim()))
            {
                this.ShowMessage("工作日输入", MsgType.NG);
                bResult = false;
            }
            try
            {
                Convert.ToDateTime(this.dtpWorkDate.Text.Trim());
            }
            catch(Exception ex)
            {
                this.ShowMessage("工作日期必须为时间格式", MsgType.NG);
                bResult = false;
            }

            return bResult;
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (!CheckInputCondition())
            {
                return;
            }
            dynamic qCondition =
                new
                {
                    work_date = Convert.ToDateTime(this.dtpWorkDate.Text.Trim().ToUpper()).ToString("yyyyMMdd"),
                    mo = this.txtMO.Text.Trim().ToUpper(),
                    model = this.txtModel.Text.Trim().ToUpper(),
                    line = this.combLineName.Text.Trim().ToUpper(),
                    group = this.combGroup.Text.Trim().ToUpper(),
                };
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();
            result = Services.ServiceMethod.GetStationRecInfo(qCondition);
            if (!result.Status)
            {
                this.ShowMessage(result.Message, MsgType.NG);
                return;
            }
            else
            {
                this.ShowMessage(result.Message, MsgType.OK);
            }
            this.datalist.DataSource = result.Data;
            if (this.datalist.Columns["mo_number"] != null)
            {
                this.datalist.Columns["mo_number"].Visible = true;
                if (string.IsNullOrEmpty(this.txtMO.Text.Trim()))
                {
                    this.datalist.Columns["mo_number"].Visible = false;
                }
            }
            if (this.datalist.Columns["model_name"] != null)
            {
                this.datalist.Columns["model_name"].Visible = true;
                if (string.IsNullOrEmpty(this.txtMO.Text.Trim()))
                {
                    this.datalist.Columns["model_name"].Visible = false;
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (this.datalist.Rows.Count > 0)
            {
                this.ExportDataToExcel(this.datalist);
                this.ShowMessage("导出Excel成功", MsgType.OK);
            }
        }

        private void cMES4001_Load(object sender, EventArgs e)
        {
            //clear input and initail group and line 
            this.ClearFormInput();            
        }
    }
}
